package org.jypj.dev.controller;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.jypj.dev.model.LoginInfo;
import org.jypj.dev.service.ILoginInfoService;
import org.jypj.dev.util.DateUtils;
import org.jypj.dev.util.ExcelHelper;
import org.jypj.dev.util.Page;
import org.jypj.dev.util.StringUtil;
import org.jypj.dev.vo.UserScoreVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 登录日志控制器
 *
 * @author ChenYu
 */
@RequestMapping(value = "loginInfoController")
@Controller
public class LoginInfoController {

    @Resource
    private ILoginInfoService loginInfoService;

    /**
     * 跳转到登录日志界面
     *
     * @return
     */
    @RequestMapping(value = "toLogList.action")
    public String toLogList(HttpServletRequest request) {
        String todayString = DateUtils.getTodayString("yyyy-MM-dd");
        request.setAttribute("startDate", todayString);
        return "back/sys/log_main";
    }

    /**
     * 分页查询日志
     *
     * @param page
     * @return
     */
    @RequestMapping(value = "selectOnePage.action")
    @ResponseBody
    public Page selectOnePage(Page page, String searchKey, String startDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("searchKey", searchKey);
        page = loginInfoService.selectOnePageByMap(page, map);
        return page;
    }

    /**
     * 查询全部的登录信息
     *
     * @param searchKey
     * @param startDate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "selectAllLog.action")
    public Map<String, Object> selectAllLog(String searchKey, String startDate) {
        Map<String, Object> dataMap = new HashMap<>();
        Map<String, Object> queryMap = new HashMap<>();
        int code = 0;
        try {
            queryMap.put("startDate", startDate);
            queryMap.put("searchKey", searchKey);
            List<LoginInfo> loginInfos = loginInfoService.selectAllByMap(queryMap);
            dataMap.put("data", loginInfos);
        } catch (Exception e) {
            e.printStackTrace();
            code = -1;
        }
        dataMap.put("code", code);
        return dataMap;
    }


    /**
     * 导出Excel
     *
     * @param startDate
     */
    @RequestMapping("exportExcel.action")
    public void exportExcel(String startDate, HttpSession session, HttpServletResponse response) {
        Map<String, Object> queryMap = new HashMap<>();
        queryMap.put("startDate", startDate);
        List<LoginInfo> loginInfos = loginInfoService.selectAllByMap(queryMap);
        exportExcel(loginInfos, startDate, session, response);
    }

    /**
     * 导出excel
     *
     * @param startDate
     * @param session
     * @param response
     */
    public void exportExcel(List<LoginInfo> loginInfos, String startDate, HttpSession session,
                            HttpServletResponse response) {

        WritableWorkbook workbook_temp = null;// Excel数据组装
        String usersTempPath = session.getServletContext().getRealPath("/static/excelModel/") + File.separator
                + "loginInfo_temp.xls";
        String userPath = session.getServletContext().getRealPath("/static/excelModel/") + File.separator
                + "loginInfo.xls";
        Workbook workbook = null; // 工作表
        try {
            workbook = Workbook.getWorkbook(new File(userPath)); // 创建一张工作表从实体路径下
            workbook_temp = Workbook.createWorkbook(new File(usersTempPath), workbook); // 拷贝一份模板
            WritableSheet sheet = workbook_temp.getSheet(0);
            sheet.addCell(new Label(0, 0, startDate + "随车机械师登录信息统计"));
            for (int i = 0; i < loginInfos.size(); i++) {
                LoginInfo loginInfo = loginInfos.get(i);
                sheet.addCell(new Label(0, 3 + i, i + 1 + ""));
                sheet.addCell(new Label(1, 3 + i, loginInfo.getUserNum()));
                sheet.addCell(new Label(2, 3 + i, loginInfo.getUserName()));
                sheet.addCell(new Label(3, 3 + i, loginInfo.getTrainCode()));
                sheet.addCell(new Label(4, 3 + i, loginInfo.getTrainNum()));
                sheet.addCell(new Label(5, 3 + i, loginInfo.getTrainType()));
                sheet.addCell(new Label(6, 3 + i, loginInfo.getRoadName()));
                sheet.addCell(new Label(7, 3 + i, loginInfo.getLoginTime()));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            // 关闭Excel工作薄对象
            if (workbook_temp != null) {
                try {
                    workbook_temp.write();
                    workbook_temp.close();
                    ExcelHelper.outputExcel(response, startDate, usersTempPath);
                    new File(usersTempPath).delete();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (workbook != null) {
                workbook.close();
            }
        }

    }

}
